/****************************************************
          File Name:dual_list.h
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Sat 07 Feb 2015 09:51:45 PM CST
*****************************************************/

#ifndef dual_list_h_
#define dual_list_h_

#include <stdlib.h>
#include <stdio.h>

#define OK      1
#define ERROR   0
typedef int status;
typedef struct LIST
{
    struct LIST *pre;
    struct LIST *next;
    char data[10];
} List;

List *init(int len)
{
    List *cur, *keep, *head=NULL;

    head = (List *)malloc(sizeof(List));
    head->pre = NULL;
    keep = head;
    for(;len--;)
    {
        cur = (List *)malloc(sizeof(List));
        if(!cur) return (List *)-1;
        keep->next = cur;
        cur->pre = keep;
        cur->next = NULL;
        keep = cur;
    }

    return head;
}

void destroy(List *head)
{
    List *cur, *tmp;

    cur = head;
    while(cur->next != NULL)
    {
        tmp = cur->next;
        printf("free: %s\n", cur->data);
        free(cur);
        cur = tmp;
    }
}

void insert(List *head, List *node)
{
    node->next = head->next;
    node->pre = head;
    head->next = node;
}

status rm(List *head)
{
    List *tmp;

    if(head->next != NULL)
    {
        tmp = head->next;
        head->next = tmp->next;
        tmp->next->pre = head;
        printf("deleting: %s\n", tmp->data);
        free(tmp);
        return OK;
    }
    else 
        return ERROR;
}
#endif
